import { Entity, Column, OneToMany, ManyToOne, JoinColumn } from "typeorm";

import {
  TickMinuteGateway,
  TickMinute,
  VARCHAR50_PG,
  SMALLINT_PG,
  INTEGER_PG,
  TickSymbol,
  TickGroupGateway,
  TickGroup,
  TickSymbolGateway,
  BIGINT_PG,
} from "qqlx-core";
import {} from "qqlx-core";

import { TransformerBigInteger, TransformerInteger, TransformerSmallInt } from "@lib/orm.transfor.number";
import { TransformerVarchar50 } from "@lib/orm.transfor.string";
import {} from "@lib/orm.transfor.enum";

import { JustIdSchema } from "@/alpha/ext.id.base";

@Entity({ name: TickGroupGateway.relations })
export class TickGroupSchema extends JustIdSchema implements TickGroup {
  @Column({ transformer: new TransformerVarchar50(), type: "varchar", nullable: false, length: 50 })
  name: VARCHAR50_PG = "";

  @Column({ transformer: new TransformerVarchar50(), type: "varchar", nullable: false, length: 50 })
  code: VARCHAR50_PG = "";

  @OneToMany("TickSymbolSchema", (s: TickSymbol) => s.justTickGroup)
  joinTickSymbolList!: TickSymbol[];

  @OneToMany("TickMinuteSchema", (s: TickMinute) => s.justTickGroup)
  joinTickMinuteList!: TickMinute[];
}

@Entity({ name: TickSymbolGateway.relations })
export class TickSymbolSchema extends JustIdSchema implements TickSymbol {
  @Column({ transformer: new TransformerVarchar50(), type: "varchar", nullable: false, length: 50 })
  name: VARCHAR50_PG = "";

  @Column({ transformer: new TransformerVarchar50(), type: "varchar", nullable: false, length: 50 })
  code: VARCHAR50_PG = "";

  @Column({ transformer: new TransformerInteger(), type: "integer" })
  refTickGroupId!: TickGroup["id"];

  @ManyToOne("TickGroupSchema", (s: TickGroup) => s.joinTickSymbolList)
  @JoinColumn({ name: "refTickGroupId", referencedColumnName: "id" })
  justTickGroup!: TickGroup;

  @OneToMany("TickMinuteSchema", (s: TickMinute) => s.justTickSymbol)
  joinTickMinuteList!: TickMinute[];
}

@Entity({ name: TickMinuteGateway.relations })
export class TickMinuteSchema extends JustIdSchema implements TickMinute {
  @Column({ transformer: new TransformerVarchar50(), type: "varchar", nullable: false, length: 50 })
  name: VARCHAR50_PG = "";

  @Column({ transformer: new TransformerVarchar50(), type: "varchar", nullable: false, length: 50 })
  code: VARCHAR50_PG = "";

  @Column({ transformer: new TransformerInteger(), type: "integer" })
  refTickGroupId!: TickGroup["id"];

  @ManyToOne("TickGroupSchema", (s: TickGroup) => s.joinTickMinuteList)
  @JoinColumn({ name: "refTickGroupId", referencedColumnName: "id" })
  justTickGroup!: TickGroup;

  @Column({ transformer: new TransformerInteger(), type: "integer" })
  refTickSymbolId!: TickSymbol["id"];

  @ManyToOne("TickSymbolSchema", (s: TickSymbol) => s.joinTickMinuteList)
  @JoinColumn({ name: "refTickSymbolId", referencedColumnName: "id" })
  justTickSymbol!: TickSymbol;

  //

  @Column({ transformer: new TransformerVarchar50(), type: "varchar", nullable: false, length: 50 })
  deliveryTime: VARCHAR50_PG = "0";

  @Column({ transformer: new TransformerSmallInt(), type: "smallint", nullable: false })
  deliveryYear!: SMALLINT_PG;

  @Column({ transformer: new TransformerSmallInt(), type: "smallint", nullable: false })
  deliveryMonth!: SMALLINT_PG;

  @Column({ transformer: new TransformerSmallInt(), type: "smallint", nullable: false })
  deliveryWeekDay!: SMALLINT_PG;

  @Column({ transformer: new TransformerSmallInt(), type: "smallint", nullable: false })
  deliveryHour!: SMALLINT_PG;

  @Column({ transformer: new TransformerSmallInt(), type: "smallint", nullable: false })
  deliveryMinute!: SMALLINT_PG;

  //

  @Column({ transformer: new TransformerBigInteger(), type: "bigint", nullable: false })
  volumn!: BIGINT_PG;

  @Column({ transformer: new TransformerBigInteger(), type: "bigint", nullable: false })
  turnover!: BIGINT_PG;

  @Column({ transformer: new TransformerInteger(), type: "bigint", nullable: false })
  trades!: INTEGER_PG;

  @Column({ transformer: new TransformerBigInteger(10000), type: "bigint", nullable: false })
  priceAvg!: BIGINT_PG;

  @Column({ transformer: new TransformerBigInteger(10000), type: "bigint", nullable: false })
  priceChange!: BIGINT_PG;

  @Column({ transformer: new TransformerBigInteger(10000), type: "bigint", nullable: false })
  priceChangeRate!: BIGINT_PG;
}
